<?php
/*
 *      ositem.php
 *      
 *      Copyright 2010 Vitaliy Rakitin <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
 
require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/osfields.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ostables.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/DB.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/GUI/nwmsg.php");

class ositem {
	
	//private $object;
	//private $owner;
	private $uuid;
	private $meta;
	private $fields;
	private $isadd = false;
	private $isedit = false;
	private $startpoint;
	//private $metafields;
	
	function __construct($uuid, $meta) {
		//$this->object = $object;
		//$this->owner = $owner;
		//$this->metafields = $fields;
		$this->uuid = $uuid;
		$this->meta = $meta;
		$this->fields = new osfields($this->meta->fields);
		foreach ($this->fields->getall() as $field) {
			$field->addObserver($this);
		}
/*
		$item = $this->getall();
		foreach ($this->object->fields->getall() as $field) {
			$this->fields->add($field->uuid,$item[$field->uuid]);
		}
*/
	}

	function __get($name) {
		switch ($name) {
			case "fields": { return $this->fields; break;}
			//case "tables": { return new ostables($this->object->uuid, $this->uuid); break;}
			case "uuid": {return $this->uuid; break;}
		}
	}
	
	function observable_before_changed($sender, &$value) {
		if (!$this->isedit) {
			$this->isedit();
		}
	}
	
	function observable_after_changed($sender, &$value) {
	}

/*
	function getall() {
		$sql = "SELECT * FROM ".$this->object." WHERE (id = '".$this->uuid."')";
		$sql .= $this->owner ? " AND (owner = '".$this->owner."');" : ";";
		$result = $_SESSION["metadata"]->database->select($sql);
		return $result[0];
	}
*/

	private function isedit() {
		$this->isedit = true;
		$this->startpoint = serialize($this->fields);
	}
	
	function cancel() {
		if ($this->isadd) {
			unset($this);
		}
		//$this->isadd = false;
		if ($this->isedit) {
			$this->fields = unserialize($this->startpoint);
			$this->isedit = false;
		}
/*
		foreach ($this->fields->getall() as $value) {
			unset($_SESSION["field"][$this->uuid][$value["id"]]);
		}
*/
	}
/*	
	function save() {
		//$sql = "SELECT * FROM ".$this->object->uuid." WHERE (id = '".$this->uuid."') AND (owner = '".$this->owner."');";
		$sql = "SELECT * FROM ".$this->object->uuid." WHERE (id = '".$this->uuid."')";
		$sql .= $this->owner ? " AND (owner = '".$this->owner."');" : ";";
		$result = $_SESSION["metadata"]->database->select($sql);
		// if present - then update
		if (isset($result[0])) {
			$action = "UPDATE ".$this->object->uuid." SET";
			foreach ($this->object->fields->getall() as $field) {
				$action .= " ". $field->uuid ." = '".addslashes($this->fields->byuuid($field->uuid)->value)."'";
				$action .= ",";
			}
			$action = substr_replace($action, "", -1, 1);
			$action .= " WHERE (id = '".$this->uuid."')";
			$action .= $this->owner ? " AND (owner = '".$this->owner."');" : ";";
		} else {
			// else - insert
			$action = "INSERT INTO ".$this->object->uuid."(";
			$action .= " id, ";
			foreach ($this->object->fields->getall() as $field) {
				$action .=  " ". $field->uuid;
				$action .= ",";
			}
			$action = substr_replace($action, ")", -1, 1);
			$action .= " VALUES(";
			$action .= " '$this->uuid',";
			foreach ($this->object->fields->getall() as $field) {
				$action .= " '".addslashes($this->fields->byuuid($field->uuid)->value)."'";
				$action .= ",";
			}
			$action = substr_replace($action, ");", -1, 1);
		}
		//new nwmsg($action);
		//new nwmsg($this->fields->byname("Значение")->value);
		//echo $action;
		$_SESSION["metadata"]->database->query($action);
	}
*/	
}
?>

